<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/1/23
 * Time: 9:30
 */
namespace app\admin\controller;
use think\Db;
class Doyen extends Base {
    /****
     * @return mixed|\think\response\Json
     * 首页
     */
    public function index(){
        if(request()->isAjax ()){
            $where['sup_status'] = 2;
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('super')
                ->where($where)->count();// 获取总条数
            $lists = Db::name('super')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('sup_id desc')
                ->select();
            foreach ($lists as $key=>&$vo){
                if($vo['sup_company_type'] ==1){
                    $vo['sup_company_name'] = '否';
                }elseif ($vo['sup_company_type'] ==2){
                    $vo['sup_company_name'] = '是';
                }
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }

    /***
     * @return mixed|\think\response\Json
     * 添加
     */
    public function add(){
        if(request()->isPost()){
            $goods = trim(input('param.goods'));
            $data['sup_user_id'] = 0;
            $data['sup_num'] = date('YmdHis') . str_pad(mt_rand(1, 9999), 4, '0', STR_PAD_LEFT);//达人编号
            $data['sup_name'] = trim(input('param.name'));
            $data['sup_age'] = trim(input('param.age'));
            $data['sup_high'] = trim(input('param.height'));
            $data['sup_goods'] = $goods;
            $data['sup_jianjie'] = trim(input('param.jianjie'));
            $data['sup_image'] = input('param.image');
            $data['photo'] = input('param.images');
            $data['sup_time'] = date('Y-m-d H:i:s',time());
            $data['sup_check_time'] = date('Y-m-d H:i:s',time());
            $data['sup_tz'] = input('param.tz');
            $data['is_super'] = input('param.super');
            $data['sup_auth'] = input('param.super');
            $data['sup_jjgs'] = input('param.jjgs');
            $data['sup_jjgslxr'] = input('param.jjgslxr');
            $data['sup_jj_phone'] = input('param.jj_phone');
            $data['sup_type'] =2;
            $data['sup_status'] =2;
            $data['sup_phone'] = input('param.dr_phone');
            $res = Db::table('up_super')->insertGetId($data);
            if($res){
                return json(['code' => 200, 'data' =>"", 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'data' => '', 'msg' => '添加失败']);
            }
        }
        $up_phone = Db::table('up_phone')->value('up_phone');
        $this->assign('up_phone',$up_phone);
        $realm = Db::table('up_realm')->select();
        $this->assign('realm',$realm);
        return $this->fetch();
    }
    /****
     * @return mixed
     * 详情
     */
    public function detail(){
        $id = input('param.id');
        $res =  Db::table('up_super')->where('sup_id',$id)->find();
        $up_phone = Db::table('up_phone')->value('up_phone');
        $realm_name = Db::table('up_realm')->where('realm_id','IN',$res['sup_goods'])->column('realm_name');
        $res['phone'] = $up_phone;
        $res['realm_name'] = implode(',',$realm_name);
        $this->assign('res',$res);
        return $this->fetch();
    }

    /****
     * @return mixed
     * 关联企业
     */
    public function relation(){
        if(request()->isPost()){
            //流水单号
            $bh = date('YmdHis') . str_pad(mt_rand(1, 9999), 4, '0', STR_PAD_LEFT);
            //TODO  缺少配置平台币比例
            $ptb = Db::table('up_about_us')->value('ptb');//查询平台币比例
            $id = input('param.id');//达人ID
            $status = input('param.status');
            $company_id = input('param.company_id');
            $info = explode(',',$company_id);
            //查询这个达人的信息
            $sup_company = Db::table('up_super')->where('sup_id',$id)->field('sup_company,sup_company_type')->find();
            if($status==1){//收费
                $money = input('param.money');
                $result = false;
                foreach ($info as $key=>$vo){//循环判断商户余额
                    $s_money = Db::table('up_shops')->where('s_id',$vo)->value('s_money');
                    if($s_money<$money*$ptb){
                        return json(['code' => 100, 'data' => '', 'msg' => '余额不足']);
                    }else{
                         $result = true;
                    }
                }
                if($result==true){//如果是true的话,减少商户的账户余额
                    foreach ($info as $key=>$vo){

                        Db::table('up_shops')->where('s_id',$vo)->setDec('s_money',$money*$ptb);
                        //插入达人分配记录
                        $res1['daren_user_id'] = $id;
                        $res1['daren_shop_id'] = $vo;
                        $res1['daren_money'] = $money;
                        $res1['daren_type'] = 1;
                        $res1['daren_time'] = date('Y-m-d H:i:s',time());
                        $res2[] = $res1;
                        //结束
                        //商家流水表
                        $res3['shop_apply_number'] = $bh;
                        $res3['shop_apply_status'] = 1;
                        $res3['shop_apply_type'] = 2;
                        $res3['shop_apply_money'] = $money;
                        $res3['shop_apply_ptb'] = $money*$ptb;
                        $res3['shop_time'] = date('Y-m-d H:i:s',time());
                        $res3['shop_id'] = $vo;
                        $res3['shop_title'] = "达人配置扣款".$money;
                        $res4[] = $res3;
                        allInNotice("扣款","达人配置扣款".$money.'元',3,$vo);
                        //结束
                        //平台营收表
                        $res5['revenue_number'] = $bh;
                        $res5['revenue_time'] = date('Y-m-d H:i:s',time());
                        $res5['revenue_type'] = 2;
                        $res5['revenue_money'] = $money;
                        $res5['revenue_mode'] = 5;
                        $res5['revenue_shop_id'] = $vo;
                        $res5['revenue_file'] = 1;
                        $res5['revenue_admin_id'] = session('uid');
                        $res6[] = $res5;
                        //结束
                    }
                    Db::table('up_daren_apply')->insertAll($res2);
                    Db::table('up_shop_apply')->insertAll($res4);
                    Db::table('up_revenue')->insertAll($res6);
                }
                if($sup_company['sup_company']!=''){
                    //如果已经存在的话,拼接数据
                    $sup_company['sup_company'] = $sup_company['sup_company'].','.$company_id;
                }else{
                    //不存在的话,直接赋值
                    $sup_company['sup_company'] = $company_id;
                }

                $data1['sup_company'] =$sup_company['sup_company'];//入驻的商户ID
                $data1['sup_rz_time'] =date('Y-m-d H:i:s',time());//入驻时间

                if($sup_company['sup_company_type']==1){//证明没有入驻
                    $data1['sup_company_type'] =2;//改成入驻状态
                }
                //更新数据
                $result_info = Db::table('up_super')->where('sup_id',$id)->update($data1);
                if($result_info){
                    return json(['code' => 200, 'data' =>"", 'msg' => '入驻成功']);
                }else{
                    return json(['code' => 100, 'data' => '', 'msg' => '入驻失败']);
                }
            }else{//免费的

                foreach ($info as $key=>$vo){
                    //插入达人分配记录
                    $res1['daren_user_id'] = $id;
                    $res1['daren_shop_id'] = $vo;
                    $res1['daren_money'] = '';
                    $res1['daren_type'] = 2;
                    $res1['daren_time'] = date('Y-m-d H:i:s',time());
                    $res2[] = $res1;
                }
                Db::table('up_daren_apply')->insertAll($res2);

                if($sup_company['sup_company']!=''){
                    //如果已经存在的话,拼接数据
                    $sup_company['sup_company'] = $sup_company['sup_company'].','.$company_id;
                }else{
                    //不存在的话,直接赋值
                    $sup_company['sup_company'] = $company_id;
                }

                $data1['sup_company'] =$sup_company['sup_company'];//入驻的商户ID
                $data1['sup_rz_time'] =date('Y-m-d H:i:s',time());//入驻时间

                if($sup_company['sup_company_type']==1){//证明没有入驻
                    $data1['sup_company_type'] =2;//改成入驻状态
                }
                //更新数据
                $result_info = Db::table('up_super')->where('sup_id',$id)->update($data1);
                if($result_info){
                    return json(['code' => 200, 'data' =>"", 'msg' => '入驻成功']);
                }else{
                    return json(['code' => 100, 'data' => '', 'msg' => '入驻失败']);
                }
            }
        }
       $id = input('param.id');
       $info = Db::table('up_super')->where('sup_id',$id)->field('sup_id,sup_company')->find();
       $where['s_id'] =array('not in',$info['sup_company']);
       $aa = Db::table('up_shops')->where($where)->field('s_id,s_name,s_money')->select();


        $this->assign('aa',$aa);
        $this->assign('info',$info);
       return $this->fetch();
    }

    /***
     * @return mixed|\think\response\Json
     * 编辑
     */
    public function update(){
        if(request()->isPost()){
            $id = input('param.id');
            $goods = trim(input('param.goods'));
            $data['sup_user_id'] = 0;
            $data['sup_num'] = date('YmdHis') . str_pad(mt_rand(1, 9999), 4, '0', STR_PAD_LEFT);//达人编号
            $data['sup_name'] = trim(input('param.name'));
            $data['sup_age'] = trim(input('param.age'));
            $data['sup_high'] = trim(input('param.height'));
            $data['sup_goods'] = $goods;
            $data['sup_jianjie'] = trim(input('param.jianjie'));
            $data['is_super'] = input('param.super');
            $data['sup_auth'] = input('param.super');
            $data['sup_jjgs'] = input('param.jjgs');
            $data['sup_jjgslxr'] = input('param.jjgslxr');
            $data['sup_jj_phone'] = input('param.jj_phone');
            if(input('param.images')!=''){
                $data['photo'] = input('param.images');
            }
            $data['sup_tz'] = input('param.tz');
            $data['sup_phone'] = input('param.dr_phone');
            $res = Db::table('up_super')->where('sup_id',$id)->update($data);
            if($res){
                return json(['code' => 200, 'data' =>"", 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'data' => '', 'msg' => '编辑失败']);
            }
        }
        $id = input('param.id');
        $info = Db::table('up_super')->where('sup_id',$id)->find();
        $up_phone = Db::table('up_phone')->value('up_phone');
        $info['phone'] = $up_phone;
        $info['sup_goods'] = explode(',',$info['sup_goods']);
        $this->assign('info',$info);
        $realm = Db::table('up_realm')->select();
        $this->assign('realm',$realm);
        return $this->fetch();
    }

    /****
     * @return \think\response\Json
     * 删除
     */
    public function del(){
        $id = input('id');
        $res = Db::table('up_super')->where('sup_id', $id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}